import Vue from 'vue'
/**
 * 获取当前时间戳
 */
export function timeF() {
	return Math.round(new Date() / 1000);
}

/**
 * 格式化日期格式 (用于兼容ios Date对象)
 */
export const formatDate = (time) => {
	// 将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式 
	return time.replace(/\-/g, "/");
}

/**
 * 对Date的扩展，将 Date 转化为指定格式的String
 * 月(Y)、月(m)、日(d)、小时(H)、分(M)、秒(S) 可以用 1-2 个占位符，
 * 例子：
 * dateFormat('YYYY-mm-dd HH:MM:SS', new Date()) ==> 2023-01-01 06:00:00
 */
export function dateFormat (fmt, date) {
	const opt = {
		"Y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"H+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"S+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	let ret
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt)
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt
}


/**
 * 将某个时间转化成时间戳
 * 时间格式：2019-05-20 00:00:00 或 2019年5月1日 00:00:00
 * 返回值：1556640000000，13位时间戳
 */
export function convertTimeStamp (time){
	// 用正则主要是把“2019-05-20 00:00:00”转换成“2019/05/20 00:00:00”兼容ios
	let newTime = time.replace(/-|\./g, "/");
	console.log(newTime);
	newTime = newTime.replace(/\./g, "-");
	newTime = newTime.replace(/年/g, "/");
	newTime = newTime.replace(/月/g, "/");
	newTime = newTime.replace(/日/g, "");
	if (newTime.length == 4) {
		newTime = newTime + "/01/01 00:00:00";
	}
	if (newTime.length == 7) {
		newTime = newTime + "/01 00:00:00";
	}
	if (newTime.length == 10) {
		newTime = newTime + " 00:00:00";
	}
	if (newTime.length == 16) {
		newTime = newTime + ":00";
	}
	return Date.parse(newTime);

}

// 时间戳转年月日
export function  timestampToYMD (unixtime, format) {
	var formateArr = ["Y", "M", "D", "h", "m", "s"];
	var returnArr = [];
	var dateTime = new Date(parseInt(unixtime));
	var year = dateTime.getFullYear();
	var month = dateTime.getMonth() + 1;
	var day = dateTime.getDate();
	var hour = dateTime.getHours();
	var minute = dateTime.getMinutes();
	var second = dateTime.getSeconds();
	returnArr.push(year);
	returnArr.push(month < 10 ? "0" + month : month);
	returnArr.push(day < 10 ? "0" + day : day);
	returnArr.push(hour < 10 ? "0" + hour : hour);
	returnArr.push(minute < 10 ? "0" + minute : minute);
	returnArr.push(second < 10 ? "0" + second : second);
	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}

//某个时间是否是今天 time格式：2020-07-19 20:33:00
export function  isToday(time){
	let newTime = time.replace(/-/g, "");
	newTime = newTime.substring(0, 8);
	var currentTime = new Date().getTime();
	currentTime = Jh_timeStampToTime(currentTime, "{y}{m}{d}");
	return newTime == currentTime;
}

//判断当前时间是否在某个时间段内  time格式：2020-07-19 20:33:00
export function  Jh_isBetweenTimes (beginTime, endTime) {
	beginTime = beginTime.replace(/-/g, "/");
	endTime = endTime.replace(/-/g, "/");
	beginTime = new Date(beginTime);
	endTime = new Date(endTime);
	let currentTime = new Date();
	if (beginTime <= currentTime && currentTime <= endTime) {
		return true;
	}
	return false;
}

//获取当前年月  time格式：2020-07
export const  getYearMonth =()=> {
	let timestamp = Date.parse(new Date());
	return Jh_timeStampToTime(timestamp, "{y}-{m}");
}

//获取 指定年的上一年 time格式：2020 | 2020年
export function  getPrevYear(time){
	let tempYear = time.substring(0, 4);
	tempYear = parseInt(tempYear);
	tempYear = tempYear - 1;
	let text = time.substring(4, 5);
	let prevTime = "";
	if (text == "年") {
		prevTime = tempYear + "年";
	} else {
		prevTime = tempYear + text;
	}
	return prevTime;
}

//获取 指定年的下一年 time格式：2020 | 2020年
export function  getNextYear (time) {
	let tempYear = time.substring(0, 4);
	tempYear = parseInt(tempYear);
	tempYear = tempYear + 1;
	let text = time.substring(4, 5);
	let nextTime = "";
	if (text == "年") {
		nextTime = tempYear + "年";
	} else {
		nextTime = tempYear + text;
	}
	return nextTime;
}

//获取 指定年月的上一年月 time格式：2020-07 | 2020年07月
export function  getPrevYearMonth (time) {
	let tempYear = time.substring(0, 4);
	let tempMonth = time.substring(5, 7);
	tempYear = parseInt(tempYear);
	tempMonth = parseInt(tempMonth);
	tempMonth = tempMonth - 1;
	if (tempMonth == 0) {
		tempYear = tempYear - 1;
		tempMonth = 12;
	}
	if (tempMonth < 10) {
		tempMonth = "0" + tempMonth;
	}
	let text = time.substring(4, 5);
	let prevTime = "";
	if (text == "年") {
		prevTime = tempYear + "年" + tempMonth + "月";
	} else {
		prevTime = tempYear + text + tempMonth;
	}
	return prevTime;
}

// 获取 指定年月的下一年月 time格式：2020-07 | 2020年07月
export function  getNextYearMonth (time) {
	let tempYear = time.substring(0, 4);
	let tempMonth = time.substring(5, 7);
	tempYear = parseInt(tempYear);
	tempMonth = parseInt(tempMonth);
	tempMonth = tempMonth + 1;
	if (tempMonth == 13) {
		tempYear = tempYear + 1;
		tempMonth = 1;
	}
	if (tempMonth < 10) {
		tempMonth = "0" + tempMonth;
	}
	let text = time.substring(4, 5);
	let nextTime = "";
	if (text == "年") {
		nextTime = tempYear + "年" + tempMonth + "月";
	} else {
		nextTime = tempYear + text + tempMonth;
	}
	return nextTime;
}

/**
 * @param time Date/String/Number
 * @description getEndTime('2021-02-12')
 * @returns '距离2021年2月12日还有118天0小时30分12秒'
 */
export function  getEndTime (time){
	var year = new Date(time).getFullYear();
	var month = new Date(time).getMonth() + 1;
	var date = new Date(time).getDate();
	var now = new Date();
	var endDate = new Date(new Date(time).toLocaleDateString());
	var leftTime = endDate.getTime() - now.getTime();
	var leftsecond = parseInt(leftTime / 1000);
	var day = Math.floor(leftsecond / (60 * 60 * 24));
	var hour = Math.floor((leftsecond - day * 24 * 60 * 60) / 3600);
	var minute = Math.floor((leftsecond - day * 24 * 60 * 60 - hour * 3600) / 60);
	var second = Math.floor(leftsecond - day * 60 * 60 * 24 - hour * 60 * 60 - minute * 60);
	return `距离${year}年${month}月${date}日还有${day}天${hour}小时${minute}分${second}秒`;
}


/**
 * @param {Object} time 2023-04-29T10:04:02.000+08:00
 */
export function  cxIsoTimeChangeFormat (time){
	let format = '';
	let date = new Date(time);
	let timestamp = Math.floor(date.getTime() / 1000);
	format = Vue.prototype.$u.timeFormat(timestamp, 'yyyy-mm-dd hh:MM:ss');
	return format;
}

/**
 * @param {Object} oldDate 前面的日期
 * @param {Object} newDate 后面的日期
 */
export function  cxValDayLength(oldDate, newDate){
	var d1 = new Date(oldDate);
	var d2 = new Date(newDate);
	var days = d2.getTime() - d1.getTime();
	var time = parseInt(days / (1000 * 60 * 60 * 24));
	return time

}


/**
 * @param {Object} timeType 转换成的时间格式,'yyyy-mm-dd hh:MM:ss'
 * @param {Object} time 原时间
 */
export function cxTimeChangeFormat(timeType, time) {
	let format = '';
	let date = new Date(time);
	let timestamp = Math.floor(date.getTime() / 1000);
	console.log(timestamp);
	
	format = Vue.prototype.$u.timeFormat(timestamp, timeType);
	return format;
}